<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * Adminhtml sales orders controller
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Controller_Action
{
    /**
     * Array of actions which can be processed without secret key validation
     *
     * @var array
     */

/**=====================================================*
*                                                       *
*              Globebill 勾兑程序(开始)                  *
*                                                       *
*======================================================*/
     const CORRECT_MESSAGE_A ='Blending Success (->Pending)';
     const CORRECT_MESSAGE_B ='Blending Success (->Failure)';
     const CORRECT_MESSAGE_C ='Blending Success (->Success)';
     const CORRECT_MESSAGE_D ='Order does not exist';
     const CORRECT_MESSAGE_E ='Incomplete incoming parameters';
     const CORRECT_MESSAGE_F ='Order too much (only query up to 100)';
     const CORRECT_MESSAGE_G ='Merchant number or gateway access number does not exist';
     const CORRECT_MESSAGE_H ='signInfo encrypted information error';
     const CORRECT_MESSAGE_I ='Your server IP is not registered';
     const CORRECT_MESSAGE_L ='System abnormalities Or Select only one option';
     const CORRECT_MESSAGE_J ='the php.ini not open extension = php_soap.dll';
     const CORRECT_MESSAGE_K ='Please select your Correct orders';
/**=====================================================*
*                                                       *
*              Globebill 勾兑程序(下面651行还有)        *
*                                                       *
*======================================================*/   

    protected $_publicActions = array('view', 'index');

    /**
     * Additional initialization
     *
     */
    protected function _construct()
    {
        $this->setUsedModuleName('Mage_Sales');
    }

    /**
     * Init layout, menu and breadcrumb
     *
     * @return Mage_Adminhtml_Sales_OrderController
     */
    protected function _initAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('sales/order')
            ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
            ->_addBreadcrumb($this->__('Orders'), $this->__('Orders'));
        return $this;
    }

    /**
     * Initialize order model instance
     *
     * @return Mage_Sales_Model_Order || false
     */
    protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }

    /**
     * Orders grid
     */
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));

        $this->_initAction()
            ->renderLayout();
    }

    /**
     * Order grid
     */
    public function gridAction()
    {
        $this->loadLayout(false);
        $this->renderLayout();
    }

    /**
     * View order detale
     */
    public function viewAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders'));

        $order = $this->_initOrder();
        if ($order) {

            $isActionsNotPermitted = $order->getActionFlag(
                Mage_Sales_Model_Order::ACTION_FLAG_PRODUCTS_PERMISSION_DENIED
            );
            if ($isActionsNotPermitted) {
                $this->_getSession()->addError($this->__('You don\'t have permissions to manage this order because of one or more products are not permitted for your website.'));
            }

            $this->_initAction();

            $this->_title(sprintf("#%s", $order->getRealOrderId()));

            $this->renderLayout();
        }
    }

    /**
     * Notify user
     */
    public function emailAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->sendNewOrderEmail();
                $historyItem = Mage::getResourceModel('sales/order_status_history_collection')
                    ->getUnnotifiedForInstance($order, Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);
                if ($historyItem) {
                    $historyItem->setIsCustomerNotified(1);
                    $historyItem->save();
                }
                $this->_getSession()->addSuccess($this->__('The order email has been sent.'));
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->addError($this->__('Failed to send the order email.'));
                Mage::logException($e);
            }
        }
        $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
    }
    /**
     * Cancel order
     */
    public function cancelAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->cancel()
                    ->save();
                $this->_getSession()->addSuccess(
                    $this->__('The order has been cancelled.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order has not been cancelled.'));
                Mage::logException($e);
            }
            $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
        }
    }

    /**
     * Hold order
     */
    public function holdAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->hold()
                    ->save();
                $this->_getSession()->addSuccess(
                    $this->__('The order has been put on hold.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order was not put on hold.'));
            }
            $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
        }
    }

    /**
     * Unhold order
     */
    public function unholdAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $order->unhold()
                    ->save();
                $this->_getSession()->addSuccess(
                    $this->__('The order has been released from holding status.')
                );
            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
            catch (Exception $e) {
                $this->_getSession()->addError($this->__('The order was not unheld.'));
            }
            $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
        }
    }

    /**
     * Manage payment state
     *
     * Either denies or approves a payment that is in "review" state
     */
    public function reviewPaymentAction()
    {
        try {
            if (!$order = $this->_initOrder()) {
                return;
            }
            $action = $this->getRequest()->getParam('action', '');
            switch ($action) {
                case 'accept':
                    $order->getPayment()->accept();
                    $message = $this->__('The payment has been accepted.');
                    break;
                case 'deny':
                    $order->getPayment()->deny();
                    $message = $this->__('The payment has been denied.');
                    break;
                case 'update':
                    $order->getPayment()
                        ->registerPaymentReviewAction(Mage_Sales_Model_Order_Payment::REVIEW_ACTION_UPDATE, true);
                    $message = $this->__('Payment update has been made.');
                    break;
                default:
                    throw new Exception(sprintf('Action "%s" is not supported.', $action));
            }
            $order->save();
            $this->_getSession()->addSuccess($message);
        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Exception $e) {
            $this->_getSession()->addError($this->__('Failed to update the payment.'));
            Mage::logException($e);
        }
        $this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
    }

    /**
     * Add order comment action
     */
    public function addCommentAction()
    {
        if ($order = $this->_initOrder()) {
            try {
                $response = false;
                $data = $this->getRequest()->getPost('history');
                $notify = isset($data['is_customer_notified']) ? $data['is_customer_notified'] : false;
                $visible = isset($data['is_visible_on_front']) ? $data['is_visible_on_front'] : false;

                $order->addStatusHistoryComment($data['comment'], $data['status'])
                    ->setIsVisibleOnFront($visible)
                    ->setIsCustomerNotified($notify);

                $comment = trim(strip_tags($data['comment']));

                $order->save();
                $order->sendOrderUpdateEmail($notify, $comment);

                $this->loadLayout('empty');
                $this->renderLayout();
            }
            catch (Mage_Core_Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $e->getMessage(),
                );
            }
            catch (Exception $e) {
                $response = array(
                    'error'     => true,
                    'message'   => $this->__('Cannot add order history.')
                );
            }
            if (is_array($response)) {
                $response = Mage::helper('core')->jsonEncode($response);
                $this->getResponse()->setBody($response);
            }
        }
    }

    /**
     * Generate invoices grid for ajax request
     */
    public function invoicesAction()
    {
        $this->_initOrder();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_invoices')->toHtml()
        );
    }

    /**
     * Generate shipments grid for ajax request
     */
    public function shipmentsAction()
    {
        $this->_initOrder();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_shipments')->toHtml()
        );
    }

    /**
     * Generate creditmemos grid for ajax request
     */
    public function creditmemosAction()
    {
        $this->_initOrder();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_creditmemos')->toHtml()
        );
    }

    /**
     * Generate order history for ajax request
     */
    public function commentsHistoryAction()
    {
        $this->_initOrder();
        $html = $this->getLayout()->createBlock('adminhtml/sales_order_view_tab_history')->toHtml();
        /* @var $translate Mage_Core_Model_Translate_Inline */
        $translate = Mage::getModel('core/translate_inline');
        if ($translate->isAllowed()) {
            $translate->processResponseBody($html);
        }
        $this->getResponse()->setBody($html);
    }

    /**
     * Cancel selected orders
     */
    public function massCancelAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids', array());
        $countCancelOrder = 0;
        $countNonCancelOrder = 0;
        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            if ($order->canCancel()) {
                $order->cancel()
                    ->save();
                $countCancelOrder++;
            } else {
                $countNonCancelOrder++;
            }
        }
        if ($countNonCancelOrder) {
            if ($countCancelOrder) {
                $this->_getSession()->addError($this->__('%s order(s) cannot be canceled', $countNonCancelOrder));
            } else {
                $this->_getSession()->addError($this->__('The order(s) cannot be canceled'));
            }
        }
        if ($countCancelOrder) {
            $this->_getSession()->addSuccess($this->__('%s order(s) have been canceled.', $countCancelOrder));
        }
        $this->_redirect('*/*/');
    }

    /**
     * Hold selected orders
     */
    public function massHoldAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids', array());
        $countHoldOrder = 0;

        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            if ($order->canHold()) {
                $order->hold()
                    ->save();
                $countHoldOrder++;
            }
        }

        $countNonHoldOrder = count($orderIds) - $countHoldOrder;

        if ($countNonHoldOrder) {
            if ($countHoldOrder) {
                $this->_getSession()->addError($this->__('%s order(s) were not put on hold.', $countNonHoldOrder));
            } else {
                $this->_getSession()->addError($this->__('No order(s) were put on hold.'));
            }
        }
        if ($countHoldOrder) {
            $this->_getSession()->addSuccess($this->__('%s order(s) have been put on hold.', $countHoldOrder));
        }

        $this->_redirect('*/*/');
    }

    /**
     * Unhold selected orders
     */
    public function massUnholdAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids', array());
        $countUnholdOrder = 0;
        $countNonUnholdOrder = 0;

        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            if ($order->canUnhold()) {
                $order->unhold()
                    ->save();
                $countUnholdOrder++;
            } else {
                $countNonUnholdOrder++;
            }
        }
        if ($countNonUnholdOrder) {
            if ($countUnholdOrder) {
                $this->_getSession()->addError($this->__('%s order(s) were not released from holding status.', $countNonUnholdOrder));
            } else {
                $this->_getSession()->addError($this->__('No order(s) were released from holding status.'));
            }
        }
        if ($countUnholdOrder) {
            $this->_getSession()->addSuccess($this->__('%s order(s) have been released from holding status.', $countUnholdOrder));
        }
        $this->_redirect('*/*/');
    }

    /**
     * Change status for selected orders
     */
    public function massStatusAction()
    {

    }

    /**
     * Print documents for selected orders
     */
    public function massPrintAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids');
        $document = $this->getRequest()->getPost('document');
    }

    /**
     * Print invoices for selected orders
     */
    public function pdfinvoicesAction(){
        $orderIds = $this->getRequest()->getPost('order_ids');
        $flag = false;
        if (!empty($orderIds)) {
            foreach ($orderIds as $orderId) {
                $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($invoices->getSize() > 0) {
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }
            }
            if ($flag) {
                return $this->_prepareDownloadResponse(
                    'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                    'application/pdf'
                );
            } else {
                $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                $this->_redirect('*/*/');
            }
        }
        $this->_redirect('*/*/');
    }


/**=====================================================*
*                                                       *
*              Globebill 勾兑程序(开始)                  *
*                                                       *
*======================================================*/

    /*==================*
     *   勾兑程序函数         *
    *==================*/
    public function change_orderby_globebill($params,$k,$id)
    {
    
        $correct_message = array(
                '-2'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_A,
                '-1'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_A,
                '0'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_B,
                '1'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_C,
                '2'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_D,
                '3'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_E,
                '4'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_F,
                '5'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_G,
                '6'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_H,
                '7'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_I,
                '8'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_L);
    
    
        $correct_error = array(
                '0'=>self::CORRECT_MESSAGE_K,
                '1'=>self::CORRECT_MESSAGE_J,
                '2'=>"%s ".self::CORRECT_MESSAGE_D,
                '3'=>"%s ".self::CORRECT_MESSAGE_E,
                '4'=>"%s ".self::CORRECT_MESSAGE_F,
                '5'=>"%s ".self::CORRECT_MESSAGE_G,
                '6'=>"%s ".self::CORRECT_MESSAGE_H,
                '7'=>"%s ".self::CORRECT_MESSAGE_I,
                '8'=>"%s ".self::CORRECT_MESSAGE_L);
    
    
        try {
			// $url = 'https://check.globebill.com/servlet/TestCustomerCheck';
			$url = 'https://processor.globebill.com/servlet/NormalCustomerCheck';
			$model='';
			switch ($k)
            {
                case 'ebanx_payment':
                    $model = Mage::getModel('ebanx/payment');
					$result_data = $this->socketPost($url,$params);
					break;
                case 'creditcard_payment':
                    $model = Mage::getModel('creditcard/payment');
					$result_data = $this->socketPost($url,$params);
                    break;
                case 'yandex_payment':
                    $model = Mage::getModel('yandex/payment');
					$result_data = $this->socketPost($url,$params);
                    break;
                case 'qiwi_payment':
                    $model = Mage::getModel('qiwi/payment');
					$result_data = $this->socketPost($url,$params);
                    break;
            }
			
            
			
            $xml = new DOMDocument();
            $xml->loadXML($result_data);
                        
            $orderNo = $xml->getElementsByTagName("orderNo")->item(0)->nodeValue;
            $tradeNo = $xml->getElementsByTagName("tradeNo")->item(0)->nodeValue;
            $queryResult = $xml->getElementsByTagName("queryResult")->item(0)->nodeValue;

			Mage::log('传参',1,"corrent.log");
			Mage::log($params,1,"corrent.log");
			Mage::log("返回报文",1,"corrent.log");
			Mage::log($result_data,1,"corrent.log");
					 
    
            $order = Mage::getModel('sales/order')->load($id);

            switch ($queryResult)
            {
                case -2:    
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['-2'],$k,$orderNo)) );              
                    break;
                case -1:
                    //待处理
                    //$order->addStatusToHistory($model->getConfigData('order_status'),Mage::helper('creditcard')->__('Correct Succeed!'));
                    //$order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['-1'],$k,$orderNo)));
                    break;      
                case 0:
                    //失败
                    $order->addStatusToHistory($model->getConfigData('order_status_payment_refused'),Mage::helper('creditcard')->__('Correct Succeed!'));
                    $order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['0'],$k,$orderNo)) );
                    break;              
                case 1:
                    //成功                
                    $order->addStatusToHistory($model->getConfigData('order_status_payment_accepted'),Mage::helper('creditcard')->__('Correct succeed!'));
                    $order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['1'],$k,$orderNo)));             
                    break;          
                case 2:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['2'],$k,$orderNo)));
                    break;
                case 3:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['3'],$k,$orderNo)));
                    break;
                case 4:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['4'],$k,$orderNo)));
                    break;
                case 5:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['5'],$k,$orderNo)));
                    break;
                case 6:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['6'],$k,$orderNo)));
                    break;
                case 7:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['7'],$k,$orderNo)));
                    break;
                default:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['8'],$k,$orderNo)));
                    break;
            }
                
            $this->_redirect('*/*/');
    
        } catch (SoapFault $fault){
            echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;
        }
    
    }
    
	/*钱海的勾兑方法*/
	public function ocean_change_orderby_globebill($params,$k,$id)
    {
    
        $correct_message = array(
                '-2'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_A,
                '-1'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_A,
                '0'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_B,
                '1'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_C,
                '2'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_D,
                '3'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_E,
                '4'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_F,
                '5'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_G,
                '6'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_H,
                '7'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_I,
                '8'=>"%s orderNo: %u ".self::CORRECT_MESSAGE_L);
    
    
        $correct_error = array(
                '0'=>self::CORRECT_MESSAGE_K,
                '1'=>self::CORRECT_MESSAGE_J,
                '2'=>"%s ".self::CORRECT_MESSAGE_D,
                '3'=>"%s ".self::CORRECT_MESSAGE_E,
                '4'=>"%s ".self::CORRECT_MESSAGE_F,
                '5'=>"%s ".self::CORRECT_MESSAGE_G,
                '6'=>"%s ".self::CORRECT_MESSAGE_H,
                '7'=>"%s ".self::CORRECT_MESSAGE_I,
                '8'=>"%s ".self::CORRECT_MESSAGE_L);
    
    
        try {
			// $url = 'https://query.oceanpayment.com/service/check/test';
			$url = 'https://query.oceanpayment.com/service/check/normal';
			
			$model='';
			switch ($k)
            {
				case 'oceanpayment_yandex':
                    $model = Mage::getModel('opyandex/payment');
					$result_data = $this->oceanSocketPost($url,$params);
                    break;
                case 'oceanpayment_creditcard':
                    $model = Mage::getModel('opcreditcard/payment');
					$result_data = $this->oceanSocketPost($url,$params);
                    break;
				case 'oceanpayment_qiwi':
                    $model = Mage::getModel('opqiwi/payment');
					$result_data = $this->oceanSocketPost($url,$params);
                    break;
            }
			
            
			
            $xml = new DOMDocument();
            $xml->loadXML($result_data);
                        
            $orderNo = $xml->getElementsByTagName("order_number")->item(0)->nodeValue;
            $tradeNo = $xml->getElementsByTagName("payment_id")->item(0)->nodeValue;
            $queryResult = $xml->getElementsByTagName("payment_results")->item(0)->nodeValue;

			Mage::log('传参',1,"corrent.log");
			Mage::log($params,1,"corrent.log");
			Mage::log("返回报文",1,"corrent.log");
			Mage::log($result_data,1,"corrent.log");
					 
    
            $order = Mage::getModel('sales/order')->load($id);

            switch ($queryResult)
            {
                case -2:
                    //待确认
                    $order->addStatusToHistory($model->getConfigData('order_status_payment_refused'),Mage::helper('creditcard')->__('Correct Success!'));
                    $order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['-2'],$k,$orderNo)) );              
                    break;
                case -1:
                    //待处理
                    //$order->addStatusToHistory($model->getConfigData('order_status'),Mage::helper('creditcard')->__('Correct Succeed!'));
                    //$order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['-1'],$k,$orderNo)));
                    break;
                case 0:
                    //失败
                    $order->addStatusToHistory($model->getConfigData('order_status_payment_refused'),Mage::helper('creditcard')->__('Correct Succeed!'));
                    $order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['0'],$k,$orderNo)) );
                    break;              
                case 1:
                    //成功                
                    $order->addStatusToHistory($model->getConfigData('order_status_payment_accepted'),Mage::helper('creditcard')->__('Correct succeed!'));
                    $order->save();
                    $this->_getSession()->addSuccess($this->__(sprintf($correct_message['1'],$k,$orderNo)));
                    break;          
                case 2:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['2'],$k,$orderNo)));
                    break;
                case 3:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['3'],$k,$orderNo)));
                    break;
                case 4:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['4'],$k,$orderNo)));
                    break;
                case 5:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['5'],$k,$orderNo)));
                    break;
                case 6:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['6'],$k,$orderNo)));
                    break;
                case 7:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['7'],$k,$orderNo)));
                    break;
                default:
                    $this->_getSession()->addError($this->__(sprintf($correct_message['8'],$k,$orderNo)));
                    break;
            }
                
            $this->_redirect('*/*/');
    
        } catch (SoapFault $fault){
            echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;
        }
    
    }
    
    /*==================*
     * 点击勾兑时的函数     *
     *==================*/
    public function correctAction(){
    
    
        $orderIds = $this->getRequest()->getPost('order_ids');
    
    
        foreach($orderIds as $orderId){
    
            $id = $orderId;
    
            $order = Mage::getModel('sales/order')->load($id);
    
            $object=$order->getPayment()->registerPaymentReviewAction(Mage_Sales_Model_Order_Payment::REVIEW_ACTION_UPDATE, true);
    
    
            $k = $object->method;  //获得支付方式
            if(!empty($k)){
    
                switch ($k)
                {
                    case 'ebanx_payment':
                        $model = Mage::getModel('ebanx/payment');
                        $merNo     = $model->getConfigData('merchant_no');
                        $gatewayNo = $model->getConfigData('gateway_no');
                        $signkey   = $model->getConfigData('signkey_code');
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$signkey);
                        $ebanx_orderstr = $order->increment_id;
                        $params=array(
                                'merNo'=>$merNo,
                                'gatewayNo'=>$gatewayNo,
                                'orderNo'=>$ebanx_orderstr,
                                'signInfo'=>$signInfo
                        );
                        $this->change_orderby_globebill($params,$k,$id); 
                        break;
                    case 'creditcard_payment':
                        $model = Mage::getModel('creditcard/payment');
                        $merNo     = $model->getConfigData('merchant_no');
                        $gatewayNo = $model->getConfigData('gateway_no');
                        $signkey   = $model->getConfigData('signkey_code');
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$signkey);
                        $creditcard_orderstr = $order->increment_id;
                        $params=array(
                                'merNo'=>$merNo,
                                'gatewayNo'=>$gatewayNo,
                                'orderNo'=>$creditcard_orderstr,
                                'signInfo'=>$signInfo
                        );
                        $this->change_orderby_globebill($params,$k,$id);
                        break;
                    case 'yandex_payment':
                        $model = Mage::getModel('yandex/payment');
                        $merNo     = $model->getConfigData('merchant_no');
                        $gatewayNo = $model->getConfigData('gateway_no');
                        $signkey   = $model->getConfigData('signkey_code');
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$signkey);
                        $yandex_orderstr = $order->increment_id;
                        $params=array(
                                'merNo'=>$merNo,
                                'gatewayNo'=>$gatewayNo,
                                'orderNo'=>$yandex_orderstr,
                                'signInfo'=>$signInfo
                        );
                        $this->change_orderby_globebill($params,$k,$id);
                        break;
                    case 'qiwi_payment':
                        $model = Mage::getModel('qiwi/payment');
                        $merNo     = $model->getConfigData('merchant_no');
                        $gatewayNo = $model->getConfigData('gateway_no');
                        $signkey   = $model->getConfigData('signkey_code');
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$signkey);
                        $qiwi_orderstr = $order->increment_id;
                        $params=array(
                                'merNo'=>$merNo,
                                'gatewayNo'=>$gatewayNo,
                                'orderNo'=>$qiwi_orderstr,
                                'signInfo'=>$signInfo
                        );
                        $this->change_orderby_globebill($params,$k,$id);
                        break;
						
					/*以下是钱海的model*/
					case 'oceanpayment_creditcard':
                        $model = Mage::getModel('opcreditcard/payment');
                        $merNo     = $model->getConfigData('account');
                        $gatewayNo = $model->getConfigData('terminal');
                        $signkey   = $model->getConfigData('securecode');
						$opcreditcard_order = $order->increment_id;
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$opcreditcard_order.$signkey);
                        $params=array(
                                'account'=>$merNo,
                                'terminal'=>$gatewayNo,
                                'order_number'=>$opcreditcard_order,
                                'signValue'=>$signInfo
                        );
                        $this->ocean_change_orderby_globebill($params,$k,$id);
                        break;
					case 'oceanpayment_yandex':
                        $model = Mage::getModel('opyandex/payment');
                        $merNo     = $model->getConfigData('account');
                        $gatewayNo = $model->getConfigData('terminal');
                        $signkey   = $model->getConfigData('securecode');
                        $opyandex_order = $order->increment_id;
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$opyandex_order.$signkey);
                        $params=array(
                                'account'=>$merNo,
                                'terminal'=>$gatewayNo,
                                'order_number'=>$opyandex_order,
                                'signValue'=>$signInfo
                        );
                        $this->ocean_change_orderby_globebill($params,$k,$id);
                        break;
					case 'oceanpayment_qiwi':
                        $model = Mage::getModel('opqiwi/payment');
                        $merNo     = $model->getConfigData('account');
                        $gatewayNo = $model->getConfigData('terminal');
                        $signkey   = $model->getConfigData('securecode');
                        $opqiwi_order = $order->increment_id;
                        $signInfo  = hash('sha256',$merNo.$gatewayNo.$opqiwi_order.$signkey);
                        $params=array(
                                'account'=>$merNo,
                                'terminal'=>$gatewayNo,
                                'order_number'=>$opqiwi_order,
                                'signValue'=>$signInfo
                        );
                        $this->ocean_change_orderby_globebill($params,$k,$id);
                        break;
                }

    
            }
    
            else{
             $this->_getSession()->addError($this->__('There is no able to blending of payment.'));
             $this->_redirect('*/*/');
             exit;
            }
    
        }

    }
    
    /*==================*
     *    Socket函数         *
    *==================*/
    function socketPost($url, $data)
    {
        $url = parse_url($url);
        if (!$url) return "couldn't parse url";
        if (!isset($url['port'])) {
            $url['port'] = "";
        }
        if (!isset($url['query'])) {
            $url['query'] = "";
        }
        $encoded = "";
        while (list($k,$v) = each($data)) {
            $encoded .= ($encoded ? "&" : "");
            $encoded .= rawurlencode($k)."=".rawurlencode($v);
        }
        $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
        if (!$fp) return "Failed to open socket to $url[host]";
    
        fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
        fputs($fp, "Host: $url[host]\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
        fputs($fp, "Content-length: " . strlen($encoded) . "\n");
        fputs($fp, "Connection: close\n\n");
    
        fputs($fp, "$encoded\n");
    
        $line = fgets($fp,1024);
        if (!preg_match("/HTTP\/1\.\d (\d{3}) ([\w\d\s+]+)/", $line)) return;
    
        $results = ""; $inheader = 1;
        while(!feof($fp)) {
            $line = fgets($fp,1024);
            if ($inheader && ($line == "\n" || $line == "\r\n")) {
                $inheader = 0;
            }
            elseif (!$inheader) {
                $results .= $line;
            }
        }
        fclose($fp);
        return $results;
    }
	
	/*钱海勾兑方法*/
	function oceanSocketPost($url, $data)
	{
		$url = parse_url($url);

		if (!$url) return "couldn't parse url";
		if (!isset($url['port'])) {
			$url['port'] = "";
		}
		if (!isset($url['query'])) {
			$url['query'] = "";
		}
		$encoded = "";
		while (list($k,$v) = each($data)) {
			$encoded .= ($encoded ? "&" : "");
			$encoded .= rawurlencode($k)."=".rawurlencode($v);
		}

		$fp = fsockopen('ssl://'.$url['host'], 443, $errno, $errstr);
		if (!$fp) return "Failed to open socket to $url[host] $port ERROR: $errno - $errstr";

		fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
		fputs($fp, "Host: $url[host]\n");
		fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
		fputs($fp, "Content-length: " . strlen($encoded) . "\n");
		fputs($fp, "Connection: close\n\n");

		fputs($fp, "$encoded\n");

		$line = fgets($fp,1024);
		if (!preg_match("/HTTP\/1\.\d (\d{3}) ([\w\d\s+]+)/", $line)) return;

		$results = ""; $inheader = 1;
		while(!feof($fp)) {
			$line = fgets($fp,1024);
			if ($inheader && ($line == "\n" || $line == "\r\n")) {
				$inheader = 0;
			}
			elseif (!$inheader) {
				$results .= $line;
			}
		}
		fclose($fp);
		return $results;
	}
    
/**=====================================================*
*                                                       *
*              Globebill 勾兑程序(结束)                  *
*                                                       *
*======================================================*/



    /**
     * Print shipments for selected orders
     */
    public function pdfshipmentsAction(){
        $orderIds = $this->getRequest()->getPost('order_ids');
        $flag = false;
        if (!empty($orderIds)) {
            foreach ($orderIds as $orderId) {
                $shipments = Mage::getResourceModel('sales/order_shipment_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($shipments->getSize()) {
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }
            }
            if ($flag) {
                return $this->_prepareDownloadResponse(
                    'packingslip'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                    'application/pdf'
                );
            } else {
                $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                $this->_redirect('*/*/');
            }
        }
        $this->_redirect('*/*/');
    }

    /**
     * Print creditmemos for selected orders
     */
    public function pdfcreditmemosAction(){
        $orderIds = $this->getRequest()->getPost('order_ids');
        $flag = false;
        if (!empty($orderIds)) {
            foreach ($orderIds as $orderId) {
                $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($creditmemos->getSize()) {
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }
            }
            if ($flag) {
                return $this->_prepareDownloadResponse(
                    'creditmemo'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
                    'application/pdf'
                );
            } else {
                $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                $this->_redirect('*/*/');
            }
        }
        $this->_redirect('*/*/');
    }

    /**
     * Print all documents for selected orders
     */
    public function pdfdocsAction(){
        $orderIds = $this->getRequest()->getPost('order_ids');
        $flag = false;
        if (!empty($orderIds)) {
            foreach ($orderIds as $orderId) {
                $invoices = Mage::getResourceModel('sales/order_invoice_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($invoices->getSize()){
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }

                $shipments = Mage::getResourceModel('sales/order_shipment_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($shipments->getSize()){
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_shipment')->getPdf($shipments);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }

                $creditmemos = Mage::getResourceModel('sales/order_creditmemo_collection')
                    ->setOrderFilter($orderId)
                    ->load();
                if ($creditmemos->getSize()) {
                    $flag = true;
                    if (!isset($pdf)){
                        $pdf = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
                    } else {
                        $pages = Mage::getModel('sales/order_pdf_creditmemo')->getPdf($creditmemos);
                        $pdf->pages = array_merge ($pdf->pages, $pages->pages);
                    }
                }
            }
            if ($flag) {
                return $this->_prepareDownloadResponse(
                    'docs'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf',
                    $pdf->render(), 'application/pdf'
                );
            } else {
                $this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
                $this->_redirect('*/*/');
            }
        }
        $this->_redirect('*/*/');
    }

    /**
     * Atempt to void the order payment
     */
    public function voidPaymentAction()
    {
        if (!$order = $this->_initOrder()) {
            return;
        }
        try {
            $order->getPayment()->void(
                new Varien_Object() // workaround for backwards compatibility
            );
            $order->save();
            $this->_getSession()->addSuccess($this->__('The payment has been voided.'));
        } catch (Mage_Core_Exception $e) {
            $this->_getSession()->addError($e->getMessage());
        } catch (Exception $e) {
            $this->_getSession()->addError($this->__('Failed to void the payment.'));
            Mage::logException($e);
        }
        $this->_redirect('*/*/view', array('order_id' => $order->getId()));
    }

    /**
     * Acl check for admin
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        $action = strtolower($this->getRequest()->getActionName());
        switch ($action) {
            case 'hold':
                $aclResource = 'sales/order/actions/hold';
                break;
            case 'unhold':
                $aclResource = 'sales/order/actions/unhold';
                break;
            case 'email':
                $aclResource = 'sales/order/actions/email';
                break;
            case 'cancel':
                $aclResource = 'sales/order/actions/cancel';
                break;
            case 'view':
                $aclResource = 'sales/order/actions/view';
                break;
            case 'addcomment':
                $aclResource = 'sales/order/actions/comment';
                break;
            case 'creditmemos':
                $aclResource = 'sales/order/actions/creditmemo';
                break;
            case 'reviewpayment':
                $aclResource = 'sales/order/actions/review_payment';
                break;
            default:
                $aclResource = 'sales/order';
                break;

        }
        return Mage::getSingleton('admin/session')->isAllowed($aclResource);
    }

    /**
     * Export order grid to CSV format
     */
    public function exportCsvAction()
    {
        $fileName   = 'orders.csv';
        $grid       = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }

    /**
     *  Export order grid to Excel XML format
     */
    public function exportExcelAction()
    {
        $fileName   = 'orders.xml';
        $grid       = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }

    /**
     * Order transactions grid ajax action
     *
     */
    public function transactionsAction()
    {
        $this->_initOrder();
        $this->loadLayout(false);
        $this->renderLayout();
    }

    /**
     * Edit order address form
     */
    public function addressAction()
    {
        $addressId = $this->getRequest()->getParam('address_id');
        $address = Mage::getModel('sales/order_address')
            ->getCollection()
            ->addFilter('entity_id', $addressId)
            ->getItemById($addressId);
        if ($address) {
            Mage::register('order_address', $address);
            $this->loadLayout();
            // Do not display VAT validation button on edit order address form
            $addressFormContainer = $this->getLayout()->getBlock('sales_order_address.form.container');
            if ($addressFormContainer) {
                $addressFormContainer->getChild('form')->setDisplayVatValidationButton(false);
            }

            $this->renderLayout();
        } else {
            $this->_redirect('*/*/');
        }
    }

    /**
     * Save order address
     */
    public function addressSaveAction()
    {
        $addressId  = $this->getRequest()->getParam('address_id');
        $address    = Mage::getModel('sales/order_address')->load($addressId);
        $data       = $this->getRequest()->getPost();
        if ($data && $address->getId()) {
            $address->addData($data);
            try {
                $address->implodeStreetAddress()
                    ->save();
                $this->_getSession()->addSuccess(Mage::helper('sales')->__('The order address has been updated.'));
                $this->_redirect('*/*/view', array('order_id'=>$address->getParentId()));
                return;
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            } catch (Exception $e) {
                $this->_getSession()->addException(
                    $e,
                    Mage::helper('sales')->__('An error occurred while updating the order address. The address has not been changed.')
                );
            }
            $this->_redirect('*/*/address', array('address_id'=>$address->getId()));
        } else {
            $this->_redirect('*/*/');
        }
    }
}
